package what.basic.textcomponent;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.undo.*;
import javax.swing.event.*;
import javax.swing.text.*;
public class Jtextareaevent extends JFrame implements UndoableEditListener,DocumentListener{
	private UndoableEdit edit;
	private JTextArea jta;
	private JTextArea message;
	private JMenuItem undoitem;
	private JMenuItem redoitem;
	public Jtextareaevent(){
		super("text area event test");
		jta=new JTextArea();
		jta.getDocument().addUndoableEditListener(this);
		jta.getDocument().addDocumentListener(this);
		message=new JTextArea();
		message.setEditable(false);
		JPanel jp=new JPanel();
		jp.setLayout(new GridLayout(1,1));
		jp.setBorder(BorderFactory.createTitledBorder("Edit Area"));
		jp.add(new JScrollPane(jta));
		JPanel p2=new JPanel();
		p2.setLayout(new GridLayout(1,1));
		p2.setBorder(BorderFactory.createTitledBorder("Message"));
		p2.add(new JScrollPane(message));
		getContentPane().setLayout(new GridLayout(2,1));
		getContentPane().add(jp);
		getContentPane().add(p2);
		JMenuBar bar=new JMenuBar();
		JMenu themenu=new JMenu("Edit");
		undoitem=new JMenuItem("Undo");
		redoitem=new JMenuItem("Redo");
		themenu.add(undoitem);
		themenu.add(redoitem);
		bar.add(themenu);
		updateMenuItem();
		setJMenuBar(bar);
		setSize(300,300);
		undoitem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				edit.undo();
				updateMenuItem();
				message.append("-Undo-\n");
			}
		});
		redoitem.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				edit.redo();
				updateMenuItem();
				message.append("-Redo-\n");
			}
		});
	}
	private void updateMenuItem() {
		if(edit!=null){
			undoitem.setEnabled(edit.canUndo());
			redoitem.setEnabled(edit.canRedo());
			undoitem.setText(edit.getUndoPresentationName());
			redoitem.setText(edit.getRedoPresentationName());
		}
		else{
			undoitem.setEnabled(false);
			redoitem.setEnabled(false);
			undoitem.setText("Undo");
			redoitem.setText("Redo");
		}	
	}
	public void undoableEditHappened(UndoableEditEvent e) {
		StringBuffer buf=new StringBuffer(200);
		edit=e.getEdit();
		buf.append("undoableedit:");
		buf.append(edit.getPresentationName());
		buf.append("\n");
		message.append(buf.toString());
		updateMenuItem();
	}
	public void showDE(DocumentEvent de){
		StringBuffer debuf=new StringBuffer(100);
		debuf.append(de.getType());
		debuf.append("Offset:");
		debuf.append(de.getOffset());
		debuf.append("Length:");
		debuf.append(de.getLength());
		Element eroot=jta.getDocument().getDefaultRootElement();
		DocumentEvent.ElementChange echange=de.getChange(eroot);
		if(echange==null){
			debuf.append("No Element Change");
		}
		else{
			debuf.append("Element change:index");
			debuf.append(echange.getIndex());
		}
		debuf.append("\n");
		message.append(debuf.toString());
	}
	@Override
	public void changedUpdate(DocumentEvent e) {
		showDE(e);
	}
	public void insertUpdate(DocumentEvent e) {
		showDE(e);	
	}
	public void removeUpdate(DocumentEvent e) {
		showDE(e);
	}
	public static void main(String[] args){
		JFrame f=new Jtextareaevent();
		f.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		f.setVisible(true);
	}
}
